/*
 * This file is part of the quickTools support library, dubbed "LucasCode"
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Net;
using System.Xml;
using System.IO;
//using System.ComponentModel;

namespace AutoUpdater
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class AutoUpdater
	{
		string[] Mirrors;
		AutoUpdaterPackage[] Packages;
		public AutoUpdater()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		
		public void LoadConfig(string URL)
		{
			//Load the xml config file
		/*	XmlDocument XmlDoc = new XmlDocument();
			HttpWebResponse Response;
			HttpWebRequest Request;
			//Retrieve the File

			Request = (HttpWebRequest)HttpWebRequest.Create(URL);
/*			//Request.Headers.Add("Translate: f"); //Commented out 11/16/2004 Matt Palmerlee, this Header is more for DAV and causes a known security issue
			if(user != null && user != "")
				Request.Credentials = new NetworkCredential(user, pass);
			else
				Request.Credentials = CredentialCache.DefaultCredentials;

			//Added 11/16/2004 For Proxy Clients, Thanks George for submitting these changes
			if(proxyEnabled == true)
				Request.Proxy = new WebProxy(proxyURL,true);


			Response = (HttpWebResponse)Request.GetResponse();

			Stream respStream = null;
			respStream = Response.GetResponseStream();

			//Load the XML from the stream
			XmlDoc.Load(respStream);*/

			XmlDocument XmlDoc = new XmlDocument();
			XmlDoc.Load("updates.xml");
			XmlNode MirrorsNode = XmlDoc.SelectSingleNode(@"//mirrors");
			Mirrors = new string[MirrorsNode.ChildNodes.Count];
			int i=0;
			foreach (XmlNode node in MirrorsNode.ChildNodes)
			{
				// Fge "/" am Ende des Hostnamens ein (fr sptere URL-Zusammensetzung wichtig)
				if (node.InnerText.EndsWith("/"))
					Mirrors[i] = node.InnerText;
				else
					Mirrors[i] = node.InnerText + "/";
				i++;
			}
			

			XmlNode PackagesNode = XmlDoc.SelectSingleNode(@"//packages");
			Packages = new AutoUpdaterPackage[PackagesNode.ChildNodes.Count];
			i=0;
			foreach (XmlNode node in PackagesNode.ChildNodes)
			{
				Packages[i] = new AutoUpdaterPackage(this, node.SelectSingleNode("title").InnerText, node.SelectSingleNode("filename").InnerText, node.SelectSingleNode("relativeurl").InnerText, new Version(node.SelectSingleNode("version").InnerText));
				i++;
			}

			//XmlNode UpdatesNode = XmlDoc.SelectSingleNode(@"//updates");
		}
	}

	public class AutoUpdaterPackage
	{
		public AutoUpdaterPackage(AutoUpdater AutoUpdater, string Title, string Filename, string RelativeURL, Version LatestVersion)
		{
			m_AutoUpdater = AutoUpdater;
			m_Title = Title;
			m_Filename = Filename;
			m_RelativeURL = RelativeURL;
			m_LatestVersion = LatestVersion;
		}
		private string m_Title;
		private string m_Filename;
		private string m_RelativeURL;

		public bool HasUpdate
		{
			get
			{
				if (m_CurrentVersion < m_LatestVersion)
					return true;
				else
					return false;
			}
		}

		public string Title
		{
			get
			{
				return m_Title;
			}
		}
		Version m_LatestVersion;
		Version m_CurrentVersion;

		AutoUpdaterPackage[] RequiredPackages;
		AutoUpdater m_AutoUpdater;
	}
}
